package Jsoup;

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

/**
 * XPath:XPath即为路径语言，它是一种用来确定XML(标准通用标记语言的子集)文档中某位置的语言
 *
 * - 使用Jsoup的Xpath需要额外导入jar包（JsoupXpath-0.3.2.jar）
 * - 查询w3cshool参考手册，使用xpath的语法完成查询（https://www.w3school.com.cn/xpath/xpath_syntax.asp）
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-03-18-15:24 15:24
 */
public class XPathDemo {
    public static void main(String[] args) throws IOException, URISyntaxException, XpathSyntaxErrorException {
        String path = JsoupDemo.class.getClassLoader().getResource("student.xml").toURI().getPath();
        Document document = Jsoup.parse(new File(path), "utf-8");
        JXDocument jxDocument = new JXDocument(document);
        //所有name标签
        List<JXNode> user = jxDocument.selN("//user");
        for (JXNode jxNode : user) {
            System.out.println(jxNode);
        }
        System.out.println("-----------------------");
        //user标签下的name
        List<JXNode> name = jxDocument.selN("//user/name");
        for (JXNode jxNode : name) {
            System.out.println(jxNode);
        }
        System.out.println("-------------------");
        //user标签下的name标签中带有id属性并且值为zhang
        List<JXNode> zhang = jxDocument.selN("//user/name[@id='zhang']");
        for (JXNode jxNode : zhang) {
            System.out.println(jxNode);
        }
    }
}
